device.keepScreenOn();
auto.waitFor();
console.show();
events.observeKey();
console.setPosition(100, 313);

launch("com.tencent.mm");
sleep(1000);
console.log("打开微信");

var round = 1;
// var ke = KeyEvent.getKeyCode();
while(round < 1000){
    log("<<<<<Round: " + round + ">>>>>");
    var toread_unmuted_groups = className("TextView").depth(14).id('op').find();
    if(! toread_unmuted_groups.empty()){
        openMsgList(toread_unmuted_groups);
        log("Completed unmuted groups!!!!");
    }else{
        log("没有未读的朋友消息");
    }
    
    var toread_muted_groups = className("ImageView").depth(14).id('bai').find();
    if(! toread_muted_groups.empty()){
        openMsgList(toread_muted_groups);
        log("Completed muted groups!!!!");
    }else{
        log("没有未读的屏蔽消息");
    }
    
    log("任务完成！");
    round++;

    //Need be Android 7.0+
    var homepage_text = className("ImageView").depth(13).id('tb').findOne(500);
    if(homepage_text != null){
        var b = homepage_text.bounds();
        // click(b.left, b.top, b.right, b.bottom);
        click(b.centerX(), b.centerY());
        click(b.centerX(), b.centerY());
        sleep(500);
    }else{
        back();
        sleep(1000);
    }
}


//打开未读消息
function openMsgList(toread_groups){
    for(var group_no = 0; group_no < toread_groups.length; group_no++){
        var group_pop = toread_groups[group_no];
        log("# Open group: " + group_pop.id() + "-" + group_pop.bounds());
        var b = group_pop.bounds();
        click(b.left, b.top, b.right, b.bottom);
        log("# 开始检测红包..." + b.left + " " + b.top + " " + b.right + " " + b.bottom);
        
        var group_name = className("TextView").depth(10).id('lt').findOne(500);
        for(var loop = 0; loop < 5; loop++){
            log("## Current group detection loop: " + loop);
            searchNewRedpacket();
            //当前页面全部检测完后上滑屏幕
            //Please avoid to use findOne() as it will jam until fine one UIObject, we could use findOne(500) or fineOnce() to instead
            var group_msg_UI = className("ListView").depth(9).id('ag').findOne(500);
            if(group_msg_UI != null & group_name != null){
                group_msg_UI.scrollUp();
                sleep(500);
            }else{
                break;
            }
        }
        if(group_name != null){
            log("# Finished detection in message group: " + group_name.text());
        }else{
            group_name = null;
            log("# Finished detection in message group: " + group_name);
        }
        back();
        sleep(1000);
    }
}


//搜索新红包并点开领取
function searchNewRedpacket(){
    var rp_msg_list = className("android.widget.TextView").id('auk').find();
    log("调试信息3" + rp_msg_list);
    if(rp_msg_list.length != 0){
        log("检测到的红包个数: " + rp_msg_list.length);
        for(var i = 0; i < rp_msg_list.length; i++){
            var rp_auk = rp_msg_list[i];
            var rp_auk_parent = rp_auk.parent();
            if(rp_auk_parent.childCount() == 1){
                log("### 发现新红包");
                var rpB = rp_auk_parent.bounds();
                click(rpB.left, rpB.top, rpB.right, rpB.bottom);
                log("成功打开红包消息");
                openNewRedPacket();
                sleep(1000);
            }else if(i == (rp_msg_list.length - 1)){
                log("当前页面已检测完");
                break;
            }else{
                log("无效红包, 跳过");
            }
        }
    }else if(rp_msg_list.empty()){
        log("未检测到红包消息" + rp_msg_list.length);
    }else{
        return;
    }
}


//领取点开的红包
function openNewRedPacket(){
    var draw = desc("开").findOne(500);
    log("调试信息2");
    if(draw != null){
        log("#### 点开新红包");
        draw.click();
        log("#### Gain a LUCKY succesfully!!!");
        sleep(1000);
        //领完返回聊天主页
        back();
    }else{
        log("过期之类无效红包");
    }
    back();
    log("返回成功");
}